﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
namespace SGUI
{
    public class Property
    {
        public enum BindingType 
        {
          OneWay,
          Both,
        }
        public object mValue;
        public object Value  
        {
            set 
            {
                object oldValue = mValue;
                mValue = value;
                for (int i = 0; i < this.SetEvents.Count; i++) 
                {
                    this.SetEvents[i](value, oldValue); 
                }
            }
            get 
            {
                for (int i = 0; i < this.GetEvents.Count; i++)
                {
                    this.GetEvents[i](mValue);
                }
                return mValue;
            }
        }

        List<SetEvent> SetEvents = new List<SetEvent>();
        List<GetEvent> GetEvents = new List<GetEvent>();
        public delegate void SetEvent(object newValue,object oldValue);
        public delegate void GetEvent(object Value);

        public void AddSetEvent(SetEvent fn) 
        {
            SetEvents.Add(fn);
        }

        public void AddGetEvent(GetEvent fn) 
        {
            this.GetEvents.Add(fn);
        }

        Property BindingProperty;
        public void Binding(Property property,Property.BindingType bindType) 
        {
            BindingProperty = property;
            if (BindingProperty != null)
            {
                if (bindType == BindingType.OneWay)
                {
                    BindingProperty.AddSetEvent(BindingPropertyChange);
                }
                else
                {
                    BindingProperty.AddSetEvent(BindingPropertyChange);
                    this.AddSetEvent(BindingProperty.BindingPropertyChange);
                }
            }
        }

        public void BindingPropertyChange(object newValue, object oldValue) 
        {
            this.Value = newValue;
        }


    }
}
